package com.usefullc.crawler.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.usefullc.common.enums.ValidActionEnum;
import com.usefullc.crawler.param.ProcessOperateParam;
import com.usefullc.system.validator.UsefulPropertyVerify;
import com.usefullc.system.validator.UsefulValid;
import com.usefullc.crawler.domain.ActiveMainInstance;
import com.usefullc.crawler.param.ActiveMainInstanceParam;
import com.usefullc.crawler.vo.ActiveMainInstanceVo;
import com.usefullc.crawler.query.ActiveMainInstanceQuery;
import com.usefullc.crawler.service.ActiveMainInstanceService;
import com.usefullc.common.util.BeanUtils;
import com.usefullc.system.controller.BaseController;
import com.usefullc.system.controller.WebResultBuilder;
import com.usefullc.system.entity.Pager;
import com.usefullc.system.entity.WebResult;
import com.usefullc.system.param.IdsParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import javax.validation.Valid;

import static com.usefullc.system.controller.WebResultBuilder.successResult;

/**
 * 爬虫主活动实例控制类
 * @author: 星空
 * @date: 2022-12-14
 */
@RestController
@RequestMapping("/crawler/activeMainInstance/")
@Api( tags="爬虫主活动实例")
public class ActiveMainInstanceController extends BaseController {

    @Autowired
    private ActiveMainInstanceService activeMainInstanceService;

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "爬虫主活动实例列表",response = WebResult.class)
    @GetMapping(path = "list",produces = "application/json")
    public WebResult list(@UsefulValid(query = true) ActiveMainInstanceQuery query) {
        Pager<ActiveMainInstanceVo> page = activeMainInstanceService.findVoByPage(query);
        return WebResultBuilder.successResult(page);
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "爬虫主活动实例所有列表", response = WebResult.class)
    @GetMapping(path = "listAll", produces = "application/json")
    public WebResult listAll(@UsefulValid(query = true) ActiveMainInstanceQuery query) {
        List<ActiveMainInstance> dataList = activeMainInstanceService.findAll(query);
        return WebResultBuilder.successResult(dataList);
    }


    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "爬虫主活动实例详情",response = WebResult.class)
    @GetMapping(path = "detail",produces = "application/json")
    public WebResult detail(@ApiParam(value = "示例ID") @Valid @RequestParam Long id) {
        ActiveMainInstance activeMainInstance = activeMainInstanceService.findById(id);
        return WebResultBuilder.successResult(activeMainInstance);
    }


    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "创建爬虫主活动实例", response = WebResult.class)
    @PostMapping(path = "/save", produces = "application/json")
    @UsefulValid
    public WebResult save(@UsefulValid(save=true) @RequestBody ActiveMainInstanceParam param) {
        ActiveMainInstance activeMainInstance = BeanUtils.beanCopy(param, ActiveMainInstance.class);
        return successResult(activeMainInstanceService.save(activeMainInstance));
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "修改爬虫主活动实例", response = WebResult.class)
    @PostMapping(path = "/update", produces = "application/json")
    public WebResult update(@UsefulValid(update=true) @RequestBody ActiveMainInstanceParam param) {
        ActiveMainInstance oldBean = activeMainInstanceService.findById(param.getId());
        BeanUtils.beanCopy(param, oldBean);
        return successResult(activeMainInstanceService.update(oldBean));
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @PostMapping(path = "del",produces = "application/json")
    @ApiOperation(value = "删除爬虫主活动实例",response = WebResult.class)
    public WebResult del(@UsefulValid(del=true) @RequestBody IdsParam idParam) {
        activeMainInstanceService.delete(idParam.getIds());
        return WebResultBuilder.successResult();
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @PostMapping(path = "delByLogic",produces = "application/json")
    @ApiOperation(value = "逻辑删除爬虫主活动实例",response = WebResult.class)
    public WebResult delByLogic(@UsefulValid(del=true) @RequestBody IdsParam idParam) {
        activeMainInstanceService.delete(idParam.getIds());
        return WebResultBuilder.successResult();
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "输出数据", response = WebResult.class)
    @PostMapping(path = "output", produces = "application/json")
    public WebResult output(@RequestBody ProcessOperateParam param) {
        Long mainActiveInstanceId = param.getMainActiveInstanceId();
        Long outputTemplateId = param.getOutputTemplateId();
        activeMainInstanceService.output(mainActiveInstanceId,outputTemplateId);
        return WebResultBuilder.successResult();
    }





}
